package com.atung.merchant.service;

import com.atung.merchant.config.auth.PwdCreateUtil;
import com.atung.merchant.config.security.JwtTokenUtil;
import com.atung.merchant.dto.AuthUserEntity;
import com.atung.merchant.mapper.BusMerchantUserMapper;
import com.atung.merchant.po.BusMerchantUser;
import com.atung.merchant.result.ApiResult;
import com.atung.merchant.vo.AuthLoginRequest;
import com.atung.merchant.vo.LoginRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author Administrator
 */
@Service
public class AuthServiceImpl {

    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private BusMerchantUserMapper busMerchantUserMapper;
    private final static String appid = "wx01945dae352857ed";

    private final static String sectet = "b359bed61f5829591db05fd64f2e0dbd";

    public ApiResult userAuthLoginService(AuthLoginRequest authLoginRequest, HttpServletRequest servletRequest) {
//        String url = (WxUrlTool.GetWxLoginUrl.replace("APPID", appid).replace("SECTET", sectet).replace("JSCODE", authLoginRequest.getCode()));
//        String response = HttpUtil.get(url);
//        WxLoginDTO loginMessage = JSONUtil.toBean(response, WxLoginDTO.class);
//        if (Objects.nonNull(loginMessage.getErrcode())) {
//            return ApiResult.fail(loginMessage.getErrcode(), loginMessage.getErrmsg());
//        }
//
//
//        String accessToken = null;
//        //获取accessToken
//        if (!stringRedisTemplate.hasKey("wx:accessToken")) {
//            String accessTokenURL = WxUrlTool.GetAccessToken.replace("APPID", appid).replace("SECRET", sectet);
//            String responsAccessToken = HttpUtil.get(accessTokenURL);
//            AccessTokenDTO accessTokenDTO = JSONUtil.toBean(responsAccessToken, AccessTokenDTO.class);
//            if (Objects.nonNull(accessTokenDTO.getAccess_token())) {
//                accessToken = accessTokenDTO.getAccess_token();
//                stringRedisTemplate.opsForValue().set("wx:accessToken", accessToken, 7000, TimeUnit.SECONDS);
//            } else {
//                return ApiResult.fail(accessTokenDTO.getErrcode(), accessTokenDTO.getErrmsg());
//            }
//        } else {
//            accessToken = stringRedisTemplate.opsForValue().get("wx:accessToken");
//        }
//
//
//        String tokenaUserType = AuthJwtUtil.createTokenaUserType("busSoundUser.getId().toString()", servletRequest);
//        stringRedisTemplate.opsForValue().set("wx:login"+busSoundUser.getId(),tokenaUserType,3,TimeUnit.DAYS);
        return ApiResult.ok();
    }

    public ApiResult userAuthLoginByNameService(LoginRequest loginRequest,HttpServletRequest servletRequest) {
        //
        loginRequest.setPwd(PwdCreateUtil.createPwd(loginRequest.getPwd()));
        QueryWrapper<BusMerchantUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",loginRequest.getPhone());
        BusMerchantUser busMerchantUser = busMerchantUserMapper.selectOne(queryWrapper);
        if (Objects.isNull(busMerchantUser)) {
            return ApiResult.noLoginName();
        }
        if (!loginRequest.getPwd().equals(busMerchantUser.getPassword())) {
            return ApiResult.pwdLoginErr();
        }
        AuthUserEntity authUserEntity = new AuthUserEntity();
        BeanUtils.copyProperties(busMerchantUser, authUserEntity);
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                new UsernamePasswordAuthenticationToken(authUserEntity, null, authUserEntity.getAuthorities());
        //安全框架封装用户认证信息
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);

        //获取用户信息方法
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AuthUserEntity jwtUser = (AuthUserEntity) (authentication.getPrincipal());
        //返回token
        String token = jwtTokenUtil.generateToken(jwtUser,servletRequest);
        return ApiResult.ok(token);

    }
}
